﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Web;
using System.Web.UI;

/// <summary>
/// ChartImage 的摘要说明
/// </summary>
public class ChartImage
{
    private Bitmap bitmap = new Bitmap(1160,1160);
    private Graphics graphics;   
    private int i = 0;
    private float Xdivs = 10, Ydivs = 10;
    private float Xorigin = 25, Yorigin = 0;
    private float ScaleX=50, ScaleY=50;
    private string myLabel;
    private float x, y, x0, y0;
    private int tag = 0;
    private Color[] corlors = {Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Black, Color.Blue, Color.Purple, Color.Wheat, Color.DarkGray, Color.DeepPink };
    private Page page;
    private DataPoint dp;
    private static ArrayList chartValues = new ArrayList();
    //private List<ArrayList> LineValues = new List<ArrayList>();
    private int yLength = 0;

    private Pen pen = new Pen(Color.Blue, 2);//定义画笔   
    private Brush blackBrush = new SolidBrush(Color.Black);//定义画刷   
    private Font axesFont = new Font("arial", 15);//定义字体   */
    

	public ChartImage(Page p, string title)
	{
        graphics = Graphics.FromImage(bitmap);
        page = p;
        tag = 0;
        p.Response.ContentType = "image/jpeg";
        graphics.FillRectangle(new SolidBrush(Color.White), 0, 0, 1160, 1160);
        graphics.DrawString(title, new Font("黑体", 16), blackBrush, 500, 20);//标题
        for (i = 0; i < corlors.Length; i++)
        {
            pen = new Pen(corlors[i], 8);
            graphics.DrawLine(pen, 105 + (i * 100), 60, 155 + (i * 100), 60);
        }
            //绘X坐标轴
            graphics.DrawLine(new Pen(Color.Black, 1), 80, 120, 1080, 120);
        for (i = 0; i <= Xdivs; i++)
        {
            x = 80 + (i * 1000) / Xdivs;
            y = 120;
            myLabel = ((i - 5) * 5).ToString();
            graphics.DrawString(myLabel, axesFont, blackBrush, x - 4, y - 25);
            graphics.DrawLine(pen, x, y + 2, x, y - 2);
        }
        //绘Y坐标轴
        graphics.DrawLine(new Pen(Color.Black, 1), 580, 120, 580, 1120);
        for (i = 0; i <= Ydivs; i++)
        {
            if (i != 0)
            {
                x = 1000 / 2 + 80;
                y = 120 + (i * 1000 / Ydivs);
                myLabel = (i * 5).ToString();
                graphics.DrawString(myLabel, axesFont, blackBrush, x - 30, y - 10);
                graphics.DrawLine(pen, x + 2, y, x - 2, y);
            }
        }        
	}

    public void AddLine(float[] data, float[] lengthdata)
    {
        chartValues.Clear();
        float x0 = 0, y0 = 0, x1 = 0, y1 = 0;
        dp = new DataPoint();
        dp.X = x0;
        dp.Y = y0;
        dp.Valide = true;
        chartValues.Add(dp);
        for (int i = 0; i < data.Length; i++)
        {
            if (data[i] == 0)
            {
                x1 = x0;
                y1 = y0 + float.Parse(lengthdata[i].ToString());
            }
            else
            {
                x1 = x0 + float.Parse((lengthdata[i] * float.Parse(Math.Sin(Math.PI * data[i] / 180).ToString())).ToString());
                y1 = y0 + float.Parse((lengthdata[i] * float.Parse(Math.Cos(Math.PI * data[i] / 180).ToString())).ToString());
            }

            dp = new DataPoint();
            dp.X = x1;
            dp.Y = y1;
            dp.Valide = true;
            chartValues.Add(dp);//保存曲线坐标
            x0 = x1;
            y0 = y1;
        }

        DrawLine(chartValues);//绘制曲线
        
    }

    //根据坐标绘制曲线
    public void DrawLine(ArrayList arr)
    {
        graphics = Graphics.FromImage(bitmap);
        pen = new Pen(corlors[tag], 4);//定义画笔
        graphics.RotateTransform(180);
        graphics.TranslateTransform(-80, -1120);
        graphics.ScaleTransform(-1, 1);

        DataPoint prevPoint = new DataPoint();
        prevPoint.Valide = false;
        foreach (DataPoint point in arr)
        {
            if (prevPoint.Valide)
            {
                x0 = 1000 * (prevPoint.X + Xorigin) / ScaleX;
                y0 = 1000 - 1000 * (prevPoint.Y - Yorigin) / ScaleY;
                x = 1000 * (point.X + Xorigin) / ScaleX;
                y = 1000 - 1000 * (point.Y - Yorigin) / ScaleY;
                graphics.DrawLine(pen, x0, y0, x, y);
                graphics.FillEllipse(blackBrush, x0 - 4, y0 - 4, 8, 8);
                graphics.FillEllipse(blackBrush, x - 4, y - 4, 8, 8);
            }
            prevPoint = point;
        }

        tag++;
        if (tag == 10)
        {
            tag = 0;
        }
    }

    public void Display()
    {
        bitmap.Save(page.Response.OutputStream, ImageFormat.Jpeg);
    }
}

public class DataPoint
{
    private float x;
    private float y;
    private bool valide;

    public float X
    {
        get { return x; }
        set { x = value; }
    }

    public float Y
    {
        get { return y; }
        set { y = value; }
    }

    public bool Valide
    {
        get { return valide; }
        set { valide = value; }
    }
}